package com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.entity.Daily;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.entity.share.DailyContent;
import com.coffeeloversupportlibrary.databinding.DailyItemBinding;
import com.coffeeloversupportlibrary.databinding.DailyShareFragmentBinding;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author puyv
 * @createTime 2021/11/14 11:12
 * @projectName CoffeeLoverSupportLibrary
 * @className DailyAdapter.java
 * @description TODO
 */
public class DailyAdapter extends RecyclerView.Adapter {
    private List<DailyContent> dataSource;

    public DailyAdapter(List<DailyContent> dataSource) {
        this.dataSource = dataSource;
    }

    public List<DailyContent> getDataSource() {
        return dataSource;
    }

    public void setDataSource(List<DailyContent> dataSource) {
        this.dataSource = dataSource;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.daily_item,parent,false);
        DailyItemBinding dailyItemBinding = DataBindingUtil.bind(view);
        return new DailyViewHolder(dailyItemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        List<ImageInfo> imageInfo=new ArrayList<>();
        DailyContent daily=dataSource.get(position);

        //   todo: 此处处理九宫格控件 所需要的数据类型 即 ImageInfo
       for(int i=0;i<daily.getPictures().size();i++){
          ImageInfo info=new ImageInfo();
          info.setThumbnailUrl(daily.getPictures().get(i).getShareImg());
          info.setBigImageUrl(daily.getPictures().get(i).getShareImg());
          imageInfo.add(info);
       }

        ((DailyViewHolder)holder).dailyItemBinding
                .userNick.setText(daily.getUserName());
        // TODO: 2021/11/30   nullpoint

        ((DailyViewHolder)holder).dailyItemBinding.dailyTime.setText(daily.getCreateTime());




        ((DailyViewHolder)holder).dailyItemBinding.textContent.setText(daily.getShareContent());

        Glide.with(holder.itemView.getContext())
                .load(daily.getUserImg())
                .into(((DailyViewHolder)holder).dailyItemBinding.userHeader);


        // TODO: 2021/11/24   九宫格控件 添加 数据适配器
        ((DailyViewHolder)holder).dailyItemBinding.gridView.setAdapter(new NineGridViewClickAdapter(((DailyViewHolder)holder).dailyItemBinding.getRoot().getContext(),imageInfo));

    }

    @Override
    public int getItemCount() {
        return dataSource.size();
    }

    static class DailyViewHolder extends RecyclerView.ViewHolder{
        DailyItemBinding dailyItemBinding;
        public DailyViewHolder(@NonNull DailyItemBinding dailyItemBinding) {
            super(dailyItemBinding.getRoot());
            this.dailyItemBinding=dailyItemBinding;
        }
    }
}
